package com.bds.portal.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bds.model.Result;
import com.bds.model.Template;
import com.bds.portal.common.result.USession;
import com.bds.portal.service.PortalService;
import com.bds.portal.service.TempletService;
import com.bds.portal.util.Const;

import shaded.org.apache.commons.lang3.StringUtils;

@Controller
@RequestMapping("/action/tmpl/t")
public class TempletAction {
	private static Logger logger = Logger.getLogger(TempletAction.class);
	@Resource
	private TempletService templetService;
	@Resource
	private PortalService portalService;

	@ResponseBody
	@RequestMapping(value = "/addTemplateDetail", method = RequestMethod.POST)
	public Result addTemplateDetail(HttpServletRequest request, HttpServletResponse response, String requestId,
			String columnName, String type, String thinkDataId, String attr, Integer orderby,String csspath) {
		Result ret = new Result();
		ret = templetService.addTemplateDetail(requestId, columnName, type, thinkDataId, attr, orderby,csspath);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/delTemplateDetail", method = RequestMethod.POST)
	public Result addTemplateDetail(HttpServletRequest request, HttpServletResponse response, String uniqueId) {
		Result ret = new Result();
		ret = templetService.delTemplateDetail(uniqueId);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/saveTemplate", method = RequestMethod.POST)
	public Result addTemplate(HttpServletRequest request, HttpServletResponse response, Template tp) {
		Result ret = new Result();
		JSONObject tpl = JSONObject.parseObject(tp.getTmpl());
		JSONArray arr = tpl.getJSONArray("arr");
		JSONArray array = new JSONArray();
		for (int i = 0; i < arr.size(); i++) {
			JSONObject item = arr.getJSONObject(i).getJSONObject("value");
			array.add(item);
		}

		// 需要验证用户是否登录
		// 若登录则取登录的用户id，否则，弹出登录框
		Object usession = request.getSession().getAttribute(Const.SESSION_KEY);

		if (usession == null) {
			ret.setCode(Const.FAIL);
			ret.setMsg("请登录");
		
			return ret;
		} else {
			USession us = (USession) usession;
			tp.setUser_id(us.getId());
			if (tp.getRows() == null) {
				tp.setRows(0);
			}

			boolean isok = false;
			if (tp != null && tp.getType() != null && tp.getType().equals("list")) {
				if (StringUtils.isNotEmpty(tp.getPage_rule())) {
					int index = tp.getPage_rule().indexOf("[[");
					int last = tp.getPage_rule().indexOf("]]");

					if (index >= 0 && last >= 0) {
						isok = true;
					} else {
						ret.setResult(Const.FAIL, "翻页规则配置错误");
					}
				} else {
					ret.setResult(Const.FAIL, "没有配置翻页规则");
				}

			} else {
				if (StringUtils.isEmpty(tp.getContent())) {
					ret.setResult(Const.FAIL, "请添加链接");
					return ret;
				}
				String line = System.getProperty("line.separator");
				String[] arrays = tp.getContent().split(line);
				if (arrays.length == 0) {
					ret.setResult(Const.FAIL, "请输入url");
					return ret;
				}

				if (arrays.length == 1) {
					arrays = tp.getContent().split("\n");
					if (arrays.length == 1) {
						arrays = tp.getContent().split("\r");
					}
				}
				tp.setUrl_list(arrays);
				isok = true;

			}
			if (isok) {
				ret = templetService.addTemplate(tp, array, us.getId());
			}

		}
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/getTemplateDetail", method = RequestMethod.POST)
	public Result getTemplateDetail(HttpServletRequest request, HttpServletResponse response, String requestId) {
		Result ret = new Result();
		ret = templetService.getTemplateDetail(requestId);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/parseElePath", method = RequestMethod.POST)
	public Result parseElePath(HttpServletRequest request, HttpServletResponse response, String listPath,
			String nodePath, String requestId, String columnName, String columnType, String attr, Integer orderby,
			Integer islike) {
		Result ret = new Result();
		ret = templetService.parseElePath(listPath, nodePath, requestId, columnName, columnType, attr, orderby, islike);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@ResponseBody
	@RequestMapping(value = "/getElePath", method = RequestMethod.POST)
	public Result getElePath(HttpServletRequest request, HttpServletResponse response, String requestId, String id) {
		Result ret = new Result();
		ret = templetService.getElePath(requestId, id);
		response.setHeader("Access-Control-Allow-Origin", "*");
		return ret;
	}

	@RequestMapping(value = "/testDetailPath")
	@ResponseBody
	public Result testDetailPath(HttpServletRequest request, HttpServletResponse response, String request_id,
			String list_path, String node_path, String type, String attr, Integer islike) {
		Result ret = new Result();
		try {

			ret = templetService.getElePathHtml(list_path, node_path, request_id, type, attr, islike);
			response.setHeader("Access-Control-Allow-Origin", "*");
		} catch (Exception e) {
			logger.error("", e);
			ret.setResult(Const.FAIL, "操作失败");
		}
		return ret;
	}

}
